﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Slja.BLL;
using Slja.BLL.Platform;

namespace FrameWork.web.Js.InputSelect
{
    /// <summary>
    /// Search 的摘要说明
    /// </summary>
    public class Search : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            /*必须符合以下格式要求：
             * <all>
             * <response value="下拉选项的value" result ="统计数据">显示信息</response>
             * <response value="下拉选项的value2" result ="统计数据">显示信息2</response>
             * <response value="下拉选项的value3" result ="统计数据">显示信息3</response>
             * </all>
             * 
             * 
             */
            context.Response.ContentType = "text/plain";
            string keyword = context.Request.Form["kw"];
            string type = context.Request["type"].ToLower();
            string strCurPage = context.Request.Form["curpage"]??"";
            int curpage = strCurPage==""?1:int.Parse(strCurPage);
            string dataResp = "";
            if (!string.IsNullOrEmpty(type))//&& !string.IsNullOrEmpty(keyword)
            {
                switch(type)
                {
                    case "city":
                        dataResp = BLLCommon.GetCityFromKeyWord(keyword);break;
                    case "client":
                        ClientBLL client = new ClientBLL();
                        string clientclass = context.Request.QueryString["clientclass"];
                        dataResp = client.GetClientFromKeyWord(keyword,clientclass);break;
                    case "linkman":
                        string strLinkManType = context.Request.QueryString["LinkManType"];//取哪类人
                        string strLinkManInfoClass = context.Request.QueryString["LinkManInfoClass"];//取人的哪类信息
                        LinkManType ManType = ConvertLinkmanType(strLinkManType);
                        LinkManInfoClass InfoCls = ConvertLinkmanInfoClass(strLinkManInfoClass);
                        LinkManBLL linkman = new LinkManBLL();

                        dataResp = linkman.GetLinkManFromKeyWord(keyword, ManType, InfoCls, curpage);
                        break;
                    case "shop":
                        string cityID = context.Request.QueryString["cityid"];
                        int cityid ;
                        if (int.TryParse(cityID, out cityid))
                        {
                            ShopBLL shop = new ShopBLL();
                            dataResp =shop.GetShopFromKeyWord(keyword, cityID);
                        }
                        break;
                    case "prjmanager":
                        string city = context.Request.QueryString["cityid"];
                        UserBLL user = new UserBLL();
                        dataResp = user.GetManagerFromCityAndKeyword(city, keyword);
                        break;
                }                
            }
            if (string.IsNullOrEmpty(dataResp.Trim()))
            {
                dataResp = "<all/>";
            }
            context.Response.Write(dataResp);
        }
        private LinkManType ConvertLinkmanType(string strLinkManType)
        {
            LinkManType ManType = LinkManType.None;
            try
            {
                if (!string.IsNullOrEmpty(strLinkManType))
                {
                    ManType = (LinkManType)Enum.Parse(typeof(LinkManType), strLinkManType, true);
                }
            }
            catch
            {
                throw new ArgumentException("QueryString参数无法识别", "LinkManType");
            }
            return ManType;
        }
        private LinkManInfoClass ConvertLinkmanInfoClass(string strLinkManInfoClass)
        {
            LinkManInfoClass InfoCls = LinkManInfoClass.lkmUserID;
            try
            {
                if (!string.IsNullOrEmpty(strLinkManInfoClass))
                {
                    InfoCls = (LinkManInfoClass)Enum.Parse(typeof(LinkManInfoClass), strLinkManInfoClass, true);
                }
            }
            catch
            {
                throw new ArgumentException("QueryString参数无法识别", "LinkManInfoClass");
            }
            return InfoCls;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}